/*
 * @Author: deliteam 462085920@qq.com
 * @Date: 2023-11-09 09:26:52
 * @Description:
 */
import { Injectable } from '@nestjs/common';
import * as Client from 'ali-oss';
import * as dayjs from 'dayjs';
@Injectable()
export class OssService {
  async getSignature() {
    // 开发环境
    // const config = {
    //   accessKeyId: 'LTAI5tK82Zzqge2W2u8Yke6V',
    //   accessKeySecret: 'uJCUQgDMkAgHvyY2GXFW23poERyw4j', // 存储桶名字
    //   bucket: 'delisteam-bucket', // 文件存储路径
    //   dir: 'nest-cms/',
    // };
    // 生产环境
    const config = {
      accessKeyId: 'LTAI5tRX4YQMPALQRLKaqNrH',
      accessKeySecret: 'Rb6ckCaxyl5UFeEkP2FHdMXL2TuwiW', // 存储桶名字
      bucket: 'jsyz2015', // 文件存储路径
      dir: 'nest-cms/',
    };
    const client = new Client(config);
    const date = new Date(); // 时长加 1 天，作为签名的有限期
    date.setDate(date.getDate() + 1);
    const policy = {
      // 设置签名的有效期，格式为Unix时间戳
      expiration: date.toISOString(),
      conditions: [
        ['content-length-range', 0, 10485760000], // 设置上传文件的大小限制
      ],
    }; // 生成签名，策略等信息
    const formData = await client.calculatePostSignature(policy); // 生成 bucket 域名，客户端将向此地址发送请求
    const location = await client.getBucketLocation();
    const host = `http://${config.bucket}.${location.location}.aliyuncs.com`; // 响应给客户端的签名和策略等信息
    return {
      expire: dayjs().add(1, 'days').unix().toString(),
      policy: formData.policy,
      signature: formData.Signature,
      accessId: formData.OSSAccessKeyId,
      host,
      dir: config.dir,
    };
  }
}
